package edu.dayu.demo1;

/**
 * 输入: a = "11", b = "10"
 * 输出: "101"
 * 思路： 拿到最长字符串的length，然后从字符串末尾开始拿每个字符，相加
 */
public class 二进制加法 {
    public static void main(String[] args) {
        String a ="1";
        String b ="1";
        System.out.println(add(a,b));
    }

    public static String add(String a,String b){
        int max = Math.max(a.length(),b.length());
        Integer t = 0;
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < max; i++) {
            Integer a1 = a.length()>i? new Integer(a.substring(a.length()-i-1,a.length()-i)):0;
            Integer b1 = b.length()>i? new Integer(b.substring(b.length()-i-1,b.length()-i)):0;
            int sum = a1+b1+t;
            if (sum>=2){
                t=1;
            }else{
                t=0;
            }
            str.insert(0,sum%2);
        }
        if (t>0){
            str.insert(0,t);
        }
        return str.toString();
    }
}
